package com.retrozelda.nyanimals;

import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;

import com.retrozelda.nyanimals.CNyanimal.NyanimalStates;

public class CRoom implements INyanObject
{
	private static final String	TAG	= CRoom.class.getSimpleName();

	CNyanimal					m_Nyanimal;
	CBackdrop					m_BackGround;
	CWindow						m_Window;

	// display bars
	CProgressBar				m_TiredBar;
	CProgressBar				m_HungerBar;

	// action buttons
	CButton						m_ExitButton;
	CButton						m_FeedButton;

	// to draw text
	CBitmapFont					m_Text;

	// utilities
	int							screenWidth;
	int							screenHeight;
	boolean						m_shouldExit;

	public void UpdateScreenData(int width, int height)
	{
		screenWidth = width;
		screenHeight = height;

		m_Nyanimal.UpdateScreenData(width, height);
	}

	public boolean ShouldExit()
	{
		return m_shouldExit;
	}
	
	public CRoom()
	{
		m_Nyanimal = new CNyanimal();
		m_BackGround = new CBackdrop();
		m_Window = new CWindow();

		m_TiredBar = new CProgressBar();
		m_HungerBar = new CProgressBar();

		m_Text = new CBitmapFont();

		m_FeedButton = new CButton();
		m_ExitButton = new CButton();
		
		m_shouldExit = false;
	}

	@Override
	public Object clone()
	{
		CRoom ret = null;
		try
		{
			ret = (CRoom)super.clone();

			ret.m_Nyanimal = (CNyanimal)m_Nyanimal.clone();
			ret.m_BackGround = (CBackdrop)m_BackGround.clone();
			ret.m_Window = (CWindow)m_Window.clone();

			ret.m_TiredBar = (CProgressBar)m_TiredBar.clone();
			ret.m_HungerBar = (CProgressBar)m_HungerBar.clone();

			ret.m_Text = (CBitmapFont)m_Text.clone();

			ret.m_FeedButton = (CButton)m_FeedButton.clone();
			ret.m_ExitButton = (CButton)m_ExitButton.clone();
		}
		catch(CloneNotSupportedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return ret;
	}

	@Override
	public void Init(NyanimalGLSurfaceView game)
	{
		Log.d(TAG, "Init Background");
		m_BackGround.Init(game);
		
		Log.d(TAG, "Init Window");
		m_Window.Init(game);
		
		Log.d(TAG, "Init Nyanimal");
		m_Nyanimal.Init(game);

		Log.d(TAG, "Set Positions");
		m_Nyanimal.SetPosition((int)(0.5f * game.getWidth()), (int)(0.75f * game.getHeight()));
		m_Window.SetPosition((int)(0.5f * game.getWidth()), (int)(0.25f * game.getHeight()));

		Log.d(TAG, "Init Progress bars");
		m_TiredBar.Init(game, Color.BLUE, Color.RED, 10, 15, 128, 16, (int)m_Nyanimal.GetMeterMax(), (int)m_Nyanimal.GetTiredMeter());
		m_HungerBar.Init(game, Color.YELLOW, Color.RED, 10, 55, 128, 16, (int)m_Nyanimal.GetMeterMax(), (int)m_Nyanimal.GetHungerMeter());

		Log.d(TAG, "Init bitmap fonts");
		m_Text.Init(game);
		m_Text.SetSize(24, 24);

		Log.d(TAG, "Init Feed Button");
		int[] buttonSize;
		m_FeedButton.Init(game);
		m_FeedButton.LoadTexture(R.drawable.feedbtn, game);
		buttonSize = m_FeedButton.GetTextureSize();
		m_FeedButton.SetPosition(buttonSize[0] + 5, game.getHeight() - buttonSize[1] - 5);
		m_FeedButton.BuildTouchBox();

		Log.d(TAG, "Init Exit Button");
		m_ExitButton.Init(game);
		m_ExitButton.LoadTexture(R.drawable.exitbtn, game);
		buttonSize = m_ExitButton.GetTextureSize();
		m_ExitButton.SetPosition(game.getWidth() - buttonSize[0] - 5, game.getHeight() - buttonSize[1] - 5);
		m_ExitButton.BuildTouchBox();
		

		Log.d(TAG, "Room init complete");
	}

	@Override
	public void Update(float deltaTime)
	{
		// set all the bars
		m_TiredBar.SetCurrentValue((int)m_Nyanimal.GetTiredMeter());
		m_HungerBar.SetCurrentValue((int)m_Nyanimal.GetHungerMeter());

		m_Nyanimal.Update(deltaTime);
		m_BackGround.Update(deltaTime);
		m_Window.Update(deltaTime);
		m_TiredBar.Update(deltaTime);
		m_HungerBar.Update(deltaTime);

		// update all the displays
		int[] barPos = m_TiredBar.getPosition();
		m_Text.DrawText("Tired: " + m_TiredBar.GetPercent() + "%", barPos[0], barPos[1]);
		barPos = m_HungerBar.getPosition();
		m_Text.DrawText("Hunger: " + m_HungerBar.GetPercent() + "%", barPos[0], barPos[1]);

		// update buttons
		m_FeedButton.Update(deltaTime);
		m_ExitButton.Update(deltaTime);
	}

	@Override
	public void Draw(CArgumentGroup arg)
	{
		m_BackGround.Draw(arg);
		m_Window.Draw(arg);
		m_Nyanimal.Draw(arg);
		m_TiredBar.Draw(arg);
		m_HungerBar.Draw(arg);
		m_Text.Draw(arg);
		m_FeedButton.Draw(arg);
		m_ExitButton.Draw(arg);
	}

	boolean	state_	= false;

	public boolean HandleTouchEvent(MotionEvent event)
	{
		switch (event.getAction())
		{
		case MotionEvent.ACTION_DOWN:

			int posX = (int)event.getX();
			int posY = (int)event.getY();

			// check if we want to feed
			boolean feedTouch = m_FeedButton.CheckTouch(posX, posY);
			boolean exitTouch = m_ExitButton.CheckTouch(posX, posY);
			if(feedTouch)
			{
				m_Nyanimal.ChangeState(NyanimalStates.EATING);
				Log.d(TAG, "Feeding via touch");
			}
			if(exitTouch)
			{
				//android.os.Process.killProcess(android.os.Process.myPid());
				m_shouldExit = true;
				Log.d(TAG, "Exiting via touch");
				return false;
			}

			break;
		}
		return true;
	}
}
